import {Component, ElementRef, Injector, OnInit, ViewChild} from '@angular/core';
import { ResumeMgeSvr} from '../../../services/resume-mge-svr.service';
import * as echarts from 'echarts';
import {CoreComponent} from 'pengesoft-ng-lib';
import {Resume} from '../../../domains/resume.domain';
import { EmployeeServiceSvr} from '../../../services/employee.service';
import { RecruitmentPlanMgeSvrService} from '../../../services/recruitment-plan-mge-svr.service';
import {RecruitmentPlan} from '../../../domains/recruitmentplan.domain';


@Component({
  selector: 'app-home-page',
  templateUrl: './home-page.component.html',
  styleUrls: ['./home-page.component.less']
})
export class HomePageComponent extends CoreComponent implements OnInit {
  data: any;
  data2: any;
  data3: any;
  newData = [];
  newData2 = [];
  newData3 = [];
  resumeStatus = [1, 2, 3, 4, 5, 6, 7, 8];
  newArr1 = ['初筛', '初试', '复试', '加试', '待入职', '拒绝', '回拒'];         // 统计图字段
  newArr2 = ['待入职', '待离职', '试用', '在职', '正式', '实习', '离职'];
  recruitPlanstatus = [1, 2, 3, 4, 5];
  newArr3 = ['待事业部审核' , '待人事部确认', '待总经理确认', '已通过', '已驳回'];
  count1 = 0;
  count2 = 0;
  count3 = 0;

  @ViewChild('box1', {static: true}) box1: ElementRef;
  @ViewChild('box2', {static: true}) box2: ElementRef;
  @ViewChild('box3', {static: true}) box3: ElementRef;
  @ViewChild('box4', {static: true}) box4: ElementRef;

  constructor(
    private injector: Injector,
    public ResumeSvr: ResumeMgeSvr,
    public emplySvr: EmployeeServiceSvr,
    public recruSvr: RecruitmentPlanMgeSvrService
  ) {
   super(injector);
  }

  ngOnInit(): void {
    this.count1 = 0;
    this.count2 = 0;
    this.count3 = 0;
    // 面试状态信息统计
    this.ResumeSvr.getDetailList(new Resume()).then(res => {
      this.data = res.map(items => {
        return items.Resume.ApplyStatus;
      });
      // tslint:disable-next-line:prefer-for-of
      for (let i = 0; i < this.resumeStatus.length; i++) {
        this.count1 = 0;
        // tslint:disable-next-line:prefer-for-of
        for (let j = 0; j < this.data.length; j++) {
          if (this.resumeStatus[i] === this.data[j]) {
            this.count1++;
          }
        }
        this.newData.push(this.count1);
      }
      this.initCharts(this.newData, this.box1, this.newArr1);
    }, err => {
      this.nzMessageSvr.error(err.Msg);
    });

    // 员工状态统计
    this.emplySvr.myGetAllEmployDto().then(res => {
      this.data2 = res.map(items => {
        return items.Employee.StateId;
      });
      // tslint:disable-next-line:prefer-for-of
      for (let i = 0; i < this.newArr2.length; i++) {
        this.count2 = 0;
        // tslint:disable-next-line:prefer-for-of
        for (let j = 0; j < this.data2.length; j++) {
          if (this.newArr2[i] === this.data2[j]) {
            this.count2++;
          }
        }
        this.newData2.push(this.count2);
      }
      this.initCharts(this.newData2, this.box2, this.newArr2);
    }, err => {
      this.nzMessageSvr.error(err.Msg);
    });

    // 招聘计划审核统计
    this.recruSvr.getDetailList(new RecruitmentPlan()).then(res => {
      this.data3 = res.map(items => {
        return items.RecruitmentPlan.CheckStatus;
      });
      // tslint:disable-next-line:prefer-for-of
      for (let i = 0; i < this.recruitPlanstatus.length; i++) {
        this.count3 = 0;
        // tslint:disable-next-line:prefer-for-of
        for (let j = 0; j < this.data3.length; j++) {
          if (this.recruitPlanstatus[i] === this.data3[j]) {
            this.count3++;
          }
        }
        this.newData3.push(this.count3);
      }
      this.initCharts(this.newData3, this.box3, this.newArr3);
    }, err => {
      this.nzMessageSvr.error(err.Msg);
    });
  }

  onValueChange(value: Date): void {
  }

  onPanelChange(change: { date: Date; mode: string }): void {
  }

  // 图表
  // tslint:disable-next-line:typedef
  initCharts(newData: any, box: any, newArry: any) {
    // 图表项赋值
    const ec = echarts as any;
    const lineChart = ec.init(box.nativeElement);
    const lineChartOption = {
      xAxis: {
        type: 'category',
        name: '应聘状态',
        data: newArry
      },
      yAxis: {
        name: '数量',
        type: 'value'
      },
      series: [{
        data: newData,
        type: 'line'
      }]
    };
    lineChart.setOption(lineChartOption);
  }
}
